*****************************************************************************************************************************************************
* PROJECT: 		WEALTH LOSSES DUE TO CORONA PANDEMIC
* DATE: 		2020-November
* CONTENT: 		Table A11
* AUTHORS: 		Tobin Hanspal, Johannes Wohlfart, Anni Weber
*****************************************************************************************************************************************************

		clear all
		set scheme lean2
		global pm = char(177)
		set more off
		gl date = c(current_date)
		graph set window fontface "cmr10"		
		
* set cd as /Replication_files_HanspalWeberWohlfart2020/

*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~						
** Table A11
*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~	
	
foreach z in netwealth_Jan20_grp  hhnetinc_2019_grp {
use data/processed/data_1, clear

	* Scale USD variables	
		foreach x in  delofin_USD_tr02 delretir_USD_tr02  delfinwealth_USD_tr02   delhhnetinc_USD_tr02 ///
		delofin_USD_tr02alt delretir_USD_tr02alt  delfinwealth_USD_tr02alt	{
			replace `x' = `x'/1000
		}
		foreach x in ofinwealth_lvl retirewealth_lvl  finwealth_Jan20 hhinc_lvl hhnetinc_2019   {
			replace `x' = `x'/1000
		}
	*


	drop if `z'==.

	preserve
		collapse (mean) del_ofin_perc_tr02 del_retir_perc_tr02  del_finwealth_perc_tr02, by(`z') 
		keep if `z' ==1|`z'==5
			foreach x in del_ofin_perc_tr02 del_retir_perc_tr02 del_finwealth_perc_tr02 {
				gen temp_`x' = `x' if `z'==5
				egen `x'M = max(temp_`x')
				drop temp*
				gen dif_`x' = `x'M-`x'
			}
			keep if `z'==1
			keep dif*
			gen uncond = 1
		save temp/uncond_`z', replace
	restore	

	preserve
		keep if finwealth_Jan20>0
		collapse (mean) del_ofin_perc_tr02 del_retir_perc_tr02  del_finwealth_perc_tr02, by(`z') 
		keep if `z' ==1|`z'==5
			foreach x in del_ofin_perc_tr02 del_retir_perc_tr02 del_finwealth_perc_tr02 {
				gen temp_`x' = `x' if `z'==5
				egen `x'M = max(temp_`x')
				drop temp*
				gen dif_`x' = `x'M-`x'
			}
			keep if `z'==1
			keep dif*
			gen finw = 1
		save temp/finw_`z', replace
	restore	

	preserve
		keep if stockinvestor==1 
		collapse (mean) del_ofin_perc_tr02 del_retir_perc_tr02  del_finwealth_perc_tr02, by(`z') 
		keep if `z' ==1|`z'==5
			foreach x in del_ofin_perc_tr02 del_retir_perc_tr02 del_finwealth_perc_tr02 {
				gen temp_`x' = `x' if `z'==5
				egen `x'M = max(temp_`x')
				drop temp*
				gen dif_`x' = `x'M-`x'
			}
			keep if `z'==1
			keep dif*
			gen stock = 1
		save temp/stock_`z', replace
	restore	


	use temp/uncond_`z', replace
	append using temp/finw_`z'
	append using temp/stock_`z'
	gen group = "`z'"
	save "temp/q1q5`z'", replace

}


* CONDIT ON EQUITY SHARE
use data/processed/data_1, clear

	* Scale USD variables	
		foreach x in  delofin_USD_tr02 delretir_USD_tr02  delfinwealth_USD_tr02   delhhnetinc_USD_tr02 ///
		delofin_USD_tr02alt delretir_USD_tr02alt  delfinwealth_USD_tr02alt	{
			replace `x' = `x'/1000
		}
		foreach x in ofinwealth_lvl retirewealth_lvl  finwealth_Jan20 hhinc_lvl hhnetinc_2019   {
			replace `x' = `x'/1000
		}
	*		
		sum riskshare_finwealth_Jan201, d
		gen riskshare_5 = .
		replace riskshare_5=1 if riskshare_finwealth_Jan201>=0 &riskshare_finwealth_Jan201<=20 & riskshare_finwealth_Jan201!=.
		replace riskshare_5=2 if riskshare_finwealth_Jan201>20 &riskshare_finwealth_Jan201<=40 & riskshare_finwealth_Jan201!=.
		replace riskshare_5=3 if riskshare_finwealth_Jan201>40 &riskshare_finwealth_Jan201<=60 & riskshare_finwealth_Jan201!=.
		replace riskshare_5=4 if riskshare_finwealth_Jan201>60 &riskshare_finwealth_Jan201<=80 & riskshare_finwealth_Jan201!=.
		replace riskshare_5=5 if riskshare_finwealth_Jan201>80 &riskshare_finwealth_Jan201<=100 & riskshare_finwealth_Jan201!=.
		tab riskshare_5, m


	keep if stockinvestor==1

preserve	
	drop if netwealth_Jan20_grp==.
	gen base = (netwealth_Jan20_grp==5)

		reg del_ofin_perc_tr02 base i.riskshare_5 if netwealth_Jan20_grp==1 | base==1, cluster(responseid)
		local coef_ofin1 = round(_b[base], 0.01)
		
		reg del_retir_perc_tr02 base i.riskshare_5 if netwealth_Jan20_grp==1| base==1, cluster(responseid)
		local coef_ret1 =round(_b[base], 0.01)
		
		reg del_finwealth_perc_tr02 base i.riskshare_5 if netwealth_Jan20_grp==1| base==1, cluster(responseid)
		local coef_total1 = round(_b[base], 0.01)
		
	clear
	set obs 1
	gen ofin = .
	gen retire = .
	gen total = .
	foreach x in 1 {
		replace ofin = `coef_ofin`x'' in `x'
		replace retire = `coef_ret`x'' in `x'
		replace total = `coef_total`x'' in `x'
	}
	gen sort = "netwealth"
	save temp/q1q5_cond_temp, replace

restore
	drop if hhnetinc_2019_grp==.
	gen base = (hhnetinc_2019_grp==5)

		reg del_ofin_perc_tr02 base i.riskshare_5 if hhnetinc_2019_grp==1 | base==1, cluster(responseid)
		local coef_ofin1 = round(_b[base], 0.01)
		
		reg del_retir_perc_tr02 base i.riskshare_5 if hhnetinc_2019_grp==1| base==1, cluster(responseid)
		local coef_ret1 =round(_b[base], 0.01)
		
		reg del_finwealth_perc_tr02 base i.riskshare_5 if hhnetinc_2019_grp==1| base==1, cluster(responseid)
		local coef_total1 = round(_b[base], 0.01)
		
	clear
	set obs 1
	gen ofin = .
	gen retire = .
	gen total = .
	foreach x in 1 {
		replace ofin = `coef_ofin`x'' in `x'
		replace retire = `coef_ret`x'' in `x'
		replace total = `coef_total`x'' in `x'
	}
	gen sort = "netincome"
	append using "temp/q1q5_cond_temp"
	save temp/cond, replace
	
use temp/q1q5netwealth_Jan20_grp, clear
append using temp/q1q5hhnetinc_2019_grp

rename dif_del_ofin_perc_tr02 ofin
rename dif_del_retir_perc_tr02 retire 
rename dif_del_finwealth_perc_tr02 total
replace group = "netwealth" if group=="netwealth_Jan20_grp"
replace group = "netincome" if group=="hhnetinc_2019_grp"
rename group sort
append using temp/cond
gen sample = "uncond" if uncond==1
replace sample = "finw" if finw==1
replace sample = "stock" if stock==1
replace sample = "cond_eq" if sample==""
drop uncond finw stock

gsort -sort -sample 

list

	
	
	
	